<?php
namespace app\index\controller;
use think\Db;
class Article extends Base
{
    
    //=============================
    //根据文章ID号查询单篇文章内容
    //=============================
    public function index()
    {
        //判断表单是否Get传值
        if(request()->isGet()){
          
          //类型转换
          $id = (int)input('id'); //var_dump($id); die;
          if(!$id||$id<0){
            return $this->error('查询参数错误,ID不合法');
          }
        }
        //文章明细查询
        $res = Db::name('article')->alias('a')
              ->join('category c','a.category = c.id','LEFT')
              ->field('a.id,a.title,a.desc,c.name_cn as category,a.category as cat_id,a.content,a.keywords,a.tags,a.author,a.status,a.thumbnail,a.clicks,a.create_time,a.update_time')
              ->where('a.id',$id)
              ->where('a.status',1)
              ->find();
        
        //文章是否存在
        if(!$res){
          return $this->error('文章待审核或不存在');
        }
        //文章计数自增
        Db::name('article')->where('id', $id)->setInc('clicks');
        
        //明细标签赋值
        $this->assign('ARTICLE',$res);
        
        //最终模板获取
        return $this->fetch();
    }
    
    /*
    控制器默认方法
    public function index()
    {
        //根据文章id查询明细
        $id = input('id');
        
        //计数自增
        db('article')->where('id', $id)->setInc('clicks');
        
        //匹配条件
        $where = "a.status=1";
        $res = \think\Db::name('article')->alias('a')
              ->join('category c','a.category = c.id','LEFT')
              ->field('a.id,a.title,c.name_cn as category,a.category as cat_id,a.content,a.keywords,a.tags,a.author,a.status,a.thumbnail,a.clicks,a.create_time')
              ->find($id);
        
        //明细标签赋值
        $this->assign('article',$res);
        
        //获取Tag标签
        $tags = $res["tags"];
        
        //根据Tag标签查找相关文章
        $related_arts = $this->get_related($tags);
        
        //相关文章标签赋值
        $this->assign('related_arts',$related_arts);
        
        //获取上下文章ID
        $prev = \think\Db::name('article')->where('id','<',$id)->where('category','=',$res['cat_id'])->order('id desc')->limit(1)->value('id');
        $next = \think\Db::name('article')->where('id','>',$id)->where('category','=',$res['cat_id'])->order('id asc')->limit(1)->value('id');
        
        //前后标签赋值
        $this->assign('prev',$prev);
        $this->assign('next',$next);
        
        //模板获取
        return $this->fetch();
    }
    
    //相关文章查询方法
    public function get_related($tags)
    {
      $arr = explode(",",$tags);
      $articles = array();
      
      //根据Tag标签循环查找获取所有相关文章
      foreach($arr as $key=>$value){
        $map["tags"] = ["like","%".$value."%"];
        $res = \think\Db::name("article")->where($map)->field('id,title,create_time')->select(); //var_dump($res);
        $articles = array_merge($articles,$res);
      }
      $articles = array_unique_assoc($articles, 'id');  //var_dump($articles); die;
      return $articles;
    }
    */
}
